package com.vancouver.knows.ui.fragment.citydiscount;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.pangu.util.StringUtil;
import com.squareup.picasso.Picasso;
import com.vancouver.knows.R;
import com.vancouver.knows.ui.activity.event.EventDetailActivity;
import com.vancouver.knows.util.AppUtils;
import com.vancouver.knows.business.bean.EventBannerInfo;

public class DiscountBannerListView extends LinearLayout {

	public DiscountBannerListView(Context context) {
		super(context);
	}

	public DiscountBannerListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public void init(List<EventBannerInfo> datas) {
		removeAllViews();
		if (datas.size() == 0) {
			return;
		}

		setOrientation(LinearLayout.VERTICAL);
		LayoutInflater layoutInflater = LayoutInflater.from(getContext());
		for (final EventBannerInfo eventBannerInfo : datas) {
			ImageView imageView = (ImageView) layoutInflater.inflate(R.layout.discount_banner_item, this, false);
			Picasso.with(getContext())
					.load(eventBannerInfo.getImage())
					.placeholder(R.drawable.no_image)
					.resize(AppUtils.getWindowWidth(getContext()),
							getContext().getResources().getDimensionPixelSize(R.dimen.event_banner_height))
					.centerCrop().into(imageView);
			imageView.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (EventBannerInfo.TYPE_WEB.equals(eventBannerInfo.getType())
							&& !StringUtil.isEmptyString(eventBannerInfo.getUrl())) {
						AppUtils.showBrower((Activity) getContext(), eventBannerInfo.getUrl());
					} else if (EventBannerInfo.TYPE_APPLICATION.equals(eventBannerInfo.getType())
							&& !StringUtil.isEmptyString(eventBannerInfo.getPostId())) {
						Intent intent = new Intent(getContext(), EventDetailActivity.class);
						intent.putExtra("postId", eventBannerInfo.getPostId());
						getContext().startActivity(intent);
					}
				}
			});
			addView(imageView);
		}

		setDividerDrawable(getResources().getDrawable(R.drawable.linearlayout_dirver));
		setShowDividers(SHOW_DIVIDER_MIDDLE);

		requestLayout();

	}
}
